package cn.livingCloud.entity.content;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.repository.AbstractReplyContentMapper;
import cn.livingCloud.repository.ReplyRuleMapper;
import cn.livingCloud.utils.DateUtils;
import cn.livingCloud.utils.UuidGenerator;
import cn.livingCloud.wechat.protocol.MsgType;

import java.io.Serializable;
import java.util.Date;

/**
 * 回复规则
 * Created by 唐旭 on 2015/12/8.
 */
public class ReplyRule implements Serializable {
    private transient ReplyRuleMapper ruleMapper = BeanProvider.getBean(ReplyRuleMapper.class);
    private transient AbstractReplyContentMapper contentMapper = BeanProvider.getBean(AbstractReplyContentMapper.class);

    //UUID
    private String uuid = UuidGenerator.generate();
    //关键字
    private String keyWord;
    //消息类型
    private MsgType msgType;
    //true:启用,false:禁用
    private boolean enable;
    //创建时间
    //yyyy-MM-dd HH:mm:ss
    private Date dateTime = DateUtils.now();

    public ReplyRule() {
    }

    public String getUuid() {
        return uuid;
    }

    public ReplyRule setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public ReplyRule setKeyWord(String keyWord) {
        this.keyWord = keyWord;
        return this;
    }

    public MsgType getMsgType() {
        return msgType;
    }

    public ReplyRule setMsgType(MsgType msgType) {
        this.msgType = msgType;
        return this;
    }

    public AbstractReplyContent getContent() {
        return contentMapper.loadContentByRule(this);
    }

    public boolean isEnable() {
        return enable;
    }

    public ReplyRule setEnable(boolean enable) {
        this.enable = enable;
        return this;
    }

    public Date getDateTime() {
        return dateTime;
    }

    public ReplyRule setDateTime(Date dateTime) {
        this.dateTime = dateTime;
        return this;
    }

    public ReplyRule save() {
        ruleMapper.save(this);
        return this;
    }

    public void delete() {
        AbstractReplyContent abstractReplyContent = contentMapper.loadContentByRule(this);
        abstractReplyContent.delete();
        ruleMapper.delete(this.uuid);
    }
}
